<?php
/**
 * 订单
 * User: ws
 * Date: 2018/7/30
 * Time: 17:56
 */

namespace app\admin\controller;
use think\Db;


                                                                    //=====================                         =========================//
                                                                //===============================            ===================================//
                                                            //====================================          =======================================//
                                                            //======================================       =========================================//
                                                            //=======================================     ==========================================//
                                                            //======================================================================================//
                                                            //======================================================================================//
                                                            //=============        ===========================================         =============//
                                                            //===============    ================================================    ===============//
                                                            //======================================================================================//
                                                            //======================================================================================//
                                                            //======================================================================================//
                                                            //=====================================================================================//
                                                              //==================================================================================//
                                                                    //=======================================================================//



use app\admin\model\OrderServiceLog as servicelog;
class Order extends Init
{
    function _initialize()
    {
        parent::_initialize();
        $this->store = model('store');
    }
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sotre_id = input('sotre_id');
            if(!empty($sotre_id)){
                $where['o.store_id'] = $sotre_id;
            }
            if(input('order_type')!=0){
                $where['o.type'] = input('order_type');
            }

            if(is_numeric(input('order_status')) === true){
                $where['o.order_status'] = input('order_status');
            }
            $where['o.order_sn|o.phone|o.name|o.consignee|o.shipping_name|u.name'] = array('like','%'.trim($key).'%');

            $list = Db::table('je_order')->alias('o')->join('je_store s','o.store_id = s.store_id')->join('je_user u','o.user_id = u.user_id')->field('u.name as uname,s.name as sname,s.address as saddress,s.phone as sphone,o.*')->where($where)->order('order_id desc')->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

            if(!empty($_GET['uid'])){
                $_GET['name'] = db('user')->where('user_id',$_GET['uid'])->value('name');
            }else{
                $_GET['name'] = '';
            }

            $store = $this->store->store_();
            return view('list',['store'=>$store['data'],'name'=>$_GET['name']]);

    }

    function order_details(){
        $id = input('id');
        if(empty($id)){
            return json($result = ['code'=>0,'msg'=>'缺少参数']);
        }

        $info = Db::table('je_order')->alias('o')->join('je_store s','o.store_id = s.store_id')->join('je_user u','o.user_id = u.user_id')->field('u.name as uname,s.name as sname,s.address as saddress,s.phone as sphone,o.*')->where("order_id={$id}")->order('order_id desc')->find();

        //实际应该退还时间
        $a = $info['overdue_number'];
        if(!empty($a)){
            $info['sj'] = $info['lease_end_time']-($info['overdue_number']*86400);
        }

        $url = config('aliyun_oss.url');
        $info['abrasion_img'] = explode(",",str_replace("/abrasion",$url."abrasion",$info['abrasion_img']));

        if(!empty($info['overdue_number'])){
            $goodsrent = db('order_goods')->where('order_id',input('id'))->column('rent');
            $info['sum_price'] = array_sum($goodsrent);
        }else{
            $info['sum_price'] = 0;
        }



        $info['goods'] = Db::table('je_order_goods')->alias('g')->join('je_goods s','g.goods_id = s.goods_id')->field('g.*,s.name as sname')->where("order_id={$id}")->select();

        foreach($info['goods'] as $key=>$time){
            if(!empty($time['code'])){
                $code = explode(",",$time['code']);
                foreach ($code as $k=>$v){
                    $barcodes[$k] = Db::name('code')->field('barcodes')->where("code_id={$v}")->find();
                }
                $code_barcodes = array_column($barcodes,"barcodes");
                $array_barcodes = implode(",",$code_barcodes);
                $info['goods'][$key]['barcodes'] = $array_barcodes;
            }
        }
        $order_pay = Db::name('order_pay')->where("order_id={$id}")->find();

        if(!empty($info['shipping_id']))
        $info['shipping_name'] = Db::name('shipping')->where("shipping_id={$info['shipping_id']}")->value('name');


        $order_log = Db::name('order_log')->where("order_id={$id}")->field('create_time,content,type,u_id')->select();
        foreach ($order_log as $key=>$val){
            if($val['type']==0){
                $order_log[$key]['name'] = Db::name('user')->where("user_id={$val['u_id']}")->value('name');
            }elseif($val['type']==1){
                $order_log[$key]['name'] = Db::name('store')->where("store_id={$val['u_id']}")->value('name');
            }elseif($val['type']==2){
                $order_log[$key]['name'] = Db::name('admin_user')->where("admin_user_id={$val['u_id']}")->value('name');
            }elseif($val['type']==3){
                $order_log[$key]['name'] = '系统';
            }
        }


        //根据文档显示，客服备注好像是平台拥有的，store_id先对应平台用户id
        $info['service'] = Db::table('je_order_service_log')->alias('l')->join('je_admin_user a','l.store_id = a.admin_user_id')->field('l.create_time,l.content,a.name')->where("order_id={$id}")->select();

        $info['order_log'] = $order_log;
        return view('details',['list'=>$info,'order_pay'=>$order_pay]);
    }



    function one(){
        if ($this->request->isAjax()) {
//            $key = input('key')?input('key'):'';
//            if(is_numeric(input('type')) === true){
//                $where['o.distribution_settlement'] = input('type');
//            }
//
//            $where['o.order_sn'] = array('like','%'.trim($key).'%');
//            $where['o.type'] = 2;
//            $where['o.order_status'] = 3;
            $list = db('commissions')->order('commissions_id desc')->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$item){
                if($item['type']==1){
                    $order = db('order')->where("order_id={$item['order_id']}")->field('add_time,end_time,distribution_settlement,order_sn')->find();
                    $list['data'][$key]['add_time'] = $order['add_time'];
                    $list['data'][$key]['end_time'] = $order['end_time'];
                    $list['data'][$key]['distribution_settlement'] = $order['distribution_settlement'];
                    $list['data'][$key]['order_sn'] = $order['order_sn'];
                }elseif($item['type']==2){
                    $order = db('card_order')->field('add_time,pay_time,card_order_sn')->where("card_order_id={$item['order_id']}")->find();
                    $list['data'][$key]['add_time'] = $order['add_time'];
                    $list['data'][$key]['end_time'] = $order['pay_time'];
                    $list['data'][$key]['distribution_settlement'] = 1;
                    $list['data'][$key]['order_sn'] = $order['card_order_sn'];
                }


                $list['data'][$key]['name'] = db('user')->where('user_id',$item['suser_id'])->value('name');
                $list['data'][$key]['uname'] = db('user')->where('user_id',$item['user_id'])->value('name');
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('one');

    }

    function two(){
        if ($this->request->isAjax()) {
//            $key = input('key')?input('key'):'';
//            if(is_numeric(input('type')) === true){
//                $where['o.distribution_settlement'] = input('type');
//            }
//
//            $where['o.order_sn'] = array('like','%'.trim($key).'%');
//            $where['o.type'] = 2;
//            $where['o.order_status'] = 3;
//            $list = Db::table('je_commissions')->alias('c')->join('je_order o','c.order_id = o.order_id')->field('o.add_time,o.end_time,o.distribution_settlement,o.order_id,o.order_sn,c.*')->where($where)->order('commissions_id desc')->paginate(input('limit'))->toArray();
            $list = db('commissions')->order('commissions_id desc')->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$item){
                if($item['type']==1){
                    $order = db('order')->where("order_id={$item['order_id']}")->field('add_time,end_time,distribution_settlement,order_sn')->find();
                    $list['data'][$key]['add_time'] = $order['add_time'];
                    $list['data'][$key]['end_time'] = $order['end_time'];
                    $list['data'][$key]['distribution_settlement'] = $order['distribution_settlement'];
                    $list['data'][$key]['order_sn'] = $order['order_sn'];
                }elseif($item['type']==2){
                    $order = db('card_order')->field('add_time,pay_time,card_order_sn')->where("card_order_id={$item['order_id']}")->find();
                    $list['data'][$key]['add_time'] = $order['add_time'];
                    $list['data'][$key]['end_time'] = $order['pay_time'];
                    $list['data'][$key]['distribution_settlement'] = 1;
                    $list['data'][$key]['order_sn'] = $order['card_order_sn'];
                }
                if(!empty($item['upper_level_id'])){
                    $list['data'][$key]['name'] = db('user')->where('user_id',$item['suser_id'])->value('name');
                    $list['data'][$key]['uname'] = db('user')->where('user_id',$item['upper_level_id'])->value('name');
                }else{
                    unset($list['data'][$key]);
                }

            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('two');

    }








    //客服日志
    function s_log(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['store_id'] =  $_SESSION['think']['admin_user']['admin_user_id'];
            if (!servicelog::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }
    }
}